![require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages](https://cdn.sanity.io/images/cgdhsj6q/production/be8ab80c8efa5907bc341c6fefe9aa20d239d890-1600x1097.png?w=400&fit=max&auto=format)
Security News
require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
@paybase/csp
Advanced tools
A library for Communicating Sequential Processes in Node.js, built on top of async/await
.
This library requires async/await
support in your Node.js runtime, so ideally node>=7.4
.
$ npm install --save @paybase/csp
or
$ yarn add @paybase/csp
Below is a trivial example of usage, that plays on the standard ping-pong example.
const { channel, put, take } = require('@paybase/csp');
const timeout = ms => new Promise(resolve => setTimeout(resolve, ms));
const wiff = channel();
const waff = channel();
const createBall = () => ({ hits: 0, status: '' });
const createBat = async (inbound, outbound) => {
while (true) {
const ball = await take(inbound); // wait for an incoming ball
ball.hits++;
ball.status = ball.status === 'wiff!' ? 'waff!' : 'wiff!';
console.log(`🎾 Ball hit ${ball.hits} time(s), ${ball.status}`);
await timeout(500); // assume it's going to take a bit to hit the ball
await put(outbound, ball); // smash the ball back
}
};
createBat(waff, wiff); // create a bat that will wiff waffs
createBat(wiff, waff); // create a bat that will waff wiffs
put(waff, createBall());
This library exposes 5 functions and one factory.
channel()
This factory method constructs a new channel
and returns it. A channel contains no publicly accessible properties, but contains information about interactions with the channel
.
const chan = channel();
put(channel, message)
-> Promise
The put
function requires the channel
on which to put the supplied message
. The put
method returns a Promise
which can be optionally awaited and will resolve when something is ready to take the message
from the channel
.
const chan = channel();
put(chan, 42);
// ...or...
await put(chan, 42);
take(channel)
-> Promise
The take
function requires the channel
to take from. The take
method returns a Promise
which should always be awaited and will resolve with a message, when a message is available.
const chan = channel();
put(chan, 42);
const msg = await take(chan); // will receive 42
alts(...channels)
-> Promise
The alts
function will race taking values from multiple channels
.
const chan1 = channel();
const chan2 = channel();
put(chan2, 42);
const msg = await alts(chan1, chan2); // will receive 42
select(Map<*, channel>|Set<channel>|Array<channel>|Object<string, channel>)
-> Promise
The select
function will race taking values from multiple channels
, similar to alts
, but will also return the key of the channel that was selected.
const chan1 = channel();
const chan2 = channel();
put(chan2, 42);
const channels = [chan1, chan2];
const result = await select(channels); // will receive [1, 42]
Works with Map
and Set
as well as with plain-old javascript arrays and objects.
drain(channel)
-> Promise
The drain
function requires a channel
which it will drain all messages from until empty, returning an array of messages.
const chan = channel();
put(chan, 42);
put(chan, 41);
put(chan, 40);
put(chan, 39);
const msgs = await drain(chan); // will receive [ 42, 41, 40, 39 ]
Contributions are welcomed and appreciated!
Feel free to get in touch if you have any questions.
Please see the LICENSE
file for more information.
FAQs
async/await communicating sequential processes
The npm package @paybase/csp receives a total of 5 weekly downloads. As such, @paybase/csp popularity was classified as not popular.
We found that @paybase/csp demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 11 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
Security News
PyPI now supports iOS and Android wheels, making it easier for Python developers to distribute mobile packages.
Security News
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.